package com.server.protocol.user;

import java.nio.ByteBuffer;

import org.apache.log4j.Logger;

import com.nio.io.IoBuffer;
import com.nio.io.Request;
import com.nio.io.Response;
import com.nio.protocol.Protocol;
import com.server.service.UserService;

public class RegisterReq extends Protocol {

  private Logger log=Logger.getLogger(this.getClass());

  private static final short PTL_ID=0x0003;

  private String username;

  private String password;

  private UserService userService=new UserService();

  @Override
  public void decode(Request req) {
    ByteBuffer buf=req.getBodyData();
    IoBuffer buffer=new IoBuffer();
    buffer.setByteBuffer(buf);
    username=buffer.getString();
    password=buffer.getString();
  }

  @Override
  public void encode(Response resp) {
  }

  @Override
  public Protocol execute() {
    log.debug("execute:" + username + "-" + password);
    userService.saveUser(username, password);
    return new RegisterResp(username, password);
  }

  @Override
  public short getProtocolId() {
    return PTL_ID;
  }
}
